//
//  CustomTextView.m
//  54.UITextView中插入图片
//
//  Created by chenleping on 2019/1/25.
//  Copyright © 2019年 DSY. All rights reserved.
//

#import "CustomTextView.h"

@interface CustomTextView()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@end

@implementation CustomTextView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpSubViewsWithFrame:frame];
    }
    return self;
}
- (void)setUpSubViewsWithFrame:(CGRect)frame{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self addSubview:btn];
    btn.frame = CGRectMake(0, 0, frame.size.width, 30);
    btn.backgroundColor = [UIColor yellowColor];
    [btn addTarget:self action:@selector(selectPic) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"选择图片" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.inputAccessoryView = btn;
}

- (void)selectPic{
    [self showImagePicker];
}
- (void)showImagePicker {
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pickerController.allowsEditing = YES;
    pickerController.delegate = self;
    
    UIViewController *rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
    [rootController presentViewController:pickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = nil;
    if ([picker allowsEditing]){ //获取用户编辑之后的图像
        image = [info objectForKey:UIImagePickerControllerEditedImage];
    } else { // 照片的元数据参数
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    
    [picker dismissViewControllerAnimated:YES completion:^{
        NSLog(@"回传Image...");
        [self setImage:image];
    }];
}

#pragma mark --- 插入图片关键代码
- (void)setImage:(UIImage *)image {
    NSLog(@"选择图片...");
    if (image == nil) {
        [self becomeFirstResponder];
        return;
    }
    
    CGFloat width = self.frame.size.width-self.textContainer.lineFragmentPadding*2;
    
    NSMutableAttributedString *mAttributedString = self.attributedText.mutableCopy;
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.bounds = CGRectMake(0, 0, width, width * image.size.height / image.size.width);
    attachment.image = image;
    
    NSMutableAttributedString *attachmentString = [[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
    [attachmentString addAttributes:[NSMutableDictionary dictionary] range:NSMakeRange(0, attachmentString.length)];
    
    [mAttributedString insertAttributedString:attachmentString atIndex:NSMaxRange(self.selectedRange)];
    //更新attributedText
    NSInteger location = NSMaxRange(self.selectedRange) + 1;
    self.attributedText = mAttributedString.copy;
    
    //回复焦点
    self.selectedRange = NSMakeRange(location, 0);
    [self becomeFirstResponder];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}


@end
